/******* THIS FILE CONSTRUCTS THE TEACHER MEASURES for the paper Individual Agency in Street-Level Bureaucrats’ Implementation of Policy Reforms: 
The Role of their Policy Evaluation and Self-Efficacy **/
cd "datapathwheredatahasbeenplaced\"
/* WORKING WITH DANISH DATA, IT MIGHT BE NESSECARY TO TRANSLATE THE FILES 


unicode analyze *
unicode encoding set ISO-8859-1 
unicode translate *



*/



/****** THIS SECTION MAKES THE TEACHER SURVEYS COMPARABLE ***/
use datapathwheredatahasbeenplaced\laerer_2014.dta, clear
cap gen year=2014
foreach i of varlist * {
local j =lower("`i'")
capture: rename `i' `j'
}


capture rename inst_nr instnr
cap drop klassetrin

save datapathwheredatahasbeenplaced\lp2014.dta, replace

use datapathwheredatahasbeenplaced\laerer_2015.dta, clear
cap gen year=2015
foreach i of varlist * {
local j =lower("`i'")
capture: rename `i' `j'
}

cap ren klasse klasse_original

capture rename inst_nr instnr
capture drop klasse1
cap drop klassetrin
save datapathwheredatahasbeenplaced\lp2015.dta, replace


use datapathwheredatahasbeenplaced\laerer_2016.dta, clear
cap gen year=2016
foreach i of varlist * {
local j =lower("`i'")
capture: rename `i' `j'
}


save datapathwheredatahasbeenplaced\lp2016.dta, replace


use datapathwheredatahasbeenplaced\laerer_2017.dta, clear
cap gen year=2017
foreach i of varlist * {
local j =lower("`i'")
capture: rename `i' `j'
}

cap rename lpq126_2016 lpq126
cap rename lpq131_2016 lpq131

save datapathwheredatahasbeenplaced\lp2017.dta, replace

use datapathwheredatahasbeenplaced\laerer_2018.dta, clear
keep if stilling==1

cap gen year=2018
foreach i of varlist * {
local j =lower("`i'")
capture: rename `i' `j'
}

destring klassetrin, replace
save datapathwheredatahasbeenplaced\lp2018.dta, replace


/***** THIS COLLECTS THE SURVEY DATA FOR TEACHERS ***/
use datapathwheredatahasbeenplaced\lp2016.dta, clear
append using datapathwheredatahasbeenplaced\lp2015.dta
append using datapathwheredatahasbeenplaced\lp2014.dta
append using datapathwheredatahasbeenplaced\lp2017.dta
append using datapathwheredatahasbeenplaced\lp2018.dta

/*** THIS ENSURES THAT WE ONLY USE OBSERVATIONS FROM TEACHERS ***/
keep if lpq05==1 | lpq05==2 | lpq05==3 | lpq05==4

mvdecode lpq152-lpq153, mv(88 99 100)
mvdecode lpq159, mv(88)

recode lpq152 lpq153 (9 8=1) (7 6=2) (5=3) (4 3=4) (2 1=5), prefix(rec_)


mvdecode lpq89-lpq93 lpq96-lpq99 lpq140 lpq141 lpq142 lpq143 lpq154 lpq157 lpq229 ///
lpq225 lpq226 lpq227 lpq128 lpq130 lpq132 lpq133 lpq134 lpq135 lpq136 lpq137 lpq138 ///
lpq139  lpq126 lpq138 lpq224 lpq225 lpq128 lpq130  lpq188 lpq184 lpq186 lpq187 lpq59 , mv(9 100)

recode lpq96  lpq98 (1 2 3=0) (4 5=1), prefix(rec_)
recode lpq234 (0=1) (2=0), prefix(rec_)

mvdecode lpq149 lpq150 lpq151 lpq158 lpq159 lpq161, mv(88 99 100)

recode lpq140 lpq141  lpq143 (6=1) (5=2) (4=3) (3=4) (2=5) (1=6), prefix(rec_)
recode lpq149 lpq150 lpq151 (9 8=1) (7 6=2) (5 4=3) (3=4) (2=5) (1=6), prefix(rec_)
recode lpq158 lpq159 lpq161 (8 7 =1) (6 5=2) (4=3) (3=4) (2=5) (1=6), prefix(rec_)


recode lpq203 (9 100=.)

foreach var of varlist lpq198 lpq199 lpq202 lpq196 lpq195 {
recode `var' (9 100=.)
} 

recode lpq149 lpq150 lpq151 (9 8=1) (7 =2) (6=3) (5=4) (4=5) (3=6) (2=7) (1=8), prefix(rec_a_)

replace lpq198_2018=. if lpq198_2018==9
replace lpq199_2018=. if lpq199_2018==9

/***THE PART FLIP THE SCALES, SUCH THAT A HIGHER NUMBER MEANS MORE **/
foreach var of varlist lpq126 lpq138 lpq224 lpq225 lpq128 lpq130 lpq89-lpq93 ///
 lpq96-lpq99 lpq140 lpq141 lpq142 lpq143 lpq154 lpq157 lpq229 ///
 lpq226 lpq227 lpq132 lpq133 lpq134 lpq135 lpq136 lpq137 ///
 lpq139 lpq149 lpq150 lpq151 lpq158 lpq159 lpq161 lpq188 lpq184 lpq186 lpq187 lpq203 lpq198 lpq199 lpq198_2018 lpq199_2018 lpq202 lpq196 lpq195 lpq59 {
su `var'
gen `var'_rec=(r(max)+1)-`var'
local x: variable label `var'
lab var `var'_rec "`x'"
}

replace lpq198_rec=lpq198_2018_rec if lpq198==. & !mi(lpq198_2018_rec) & year==2018
replace lpq199_rec=lpq199_2018_rec if lpq199==. & !mi(lpq199_2018_rec) & year==2018

/****** THIS MAKES THE FACTOR-ANALYSIS FOR THE DEPENDENT VARIABLES AND PRODUCES THE REPORTED ALPHA AND EIGEN-VALUES
 IN TABLE 1 ***/

/*** Variation in teaching **/
factor lpq89_rec lpq90_rec lpq91_rec lpq93_rec, ml
rotate, oblique oblimin
alpha lpq89_rec lpq90_rec lpq91_rec lpq93_rec, item
estadd scalar alpha=r(alpha)
estadd matrix alphau=r(Alpha)
esttab using faktors_lp.rtf, cells("r_Ev(fmt(3)) r_L[1](t label(Variation in teaching)) alphau(fmt(3)) Psi[Uniqueness]") stats(alpha) nogap noobs label replace

/***** Compliance with National Goals ***/
factor lpq96_rec lpq98_rec lpq99_rec, ml
 rotate, oblique oblimin
 alpha lpq96_rec lpq98_rec lpq99_rec, item
 estadd scalar alpha=r(alpha)
 estadd matrix alphau=r(Alpha)
esttab using faktors_lp.rtf, cells("r_Ev(fmt(3)) r_L[1](t label(Compliance with National Goals)) alphau(fmt(3)) Psi[Uniqueness]") stats(alpha) nogap noobs label append

/**** Individual Learning Goals ***/
factor rec_lpq140 rec_lpq158 rec_lpq159 rec_lpq161, ml
rotate, oblique oblimin
alpha rec_lpq140 rec_lpq158 rec_lpq159 rec_lpq161, item
estadd scalar alpha=r(alpha)
 estadd matrix alphau=r(Alpha)
esttab using faktors_lp.rtf, cells("r_Ev(fmt(3)) r_L[1](t label(Individual Learning Goals)) alphau(fmt(3)) Psi[Uniqueness]") stats(alpha) nogap noobs label append

/***** Individualized Teaching ***/
factor rec_lpq149 rec_lpq150 rec_lpq151, ml
rotate, oblique oblimin
alpha rec_lpq149 rec_lpq150 rec_lpq151, item
estadd scalar alpha=r(alpha)
 estadd matrix alphau=r(Alpha)
esttab using faktors_lp.rtf, cells("r_Ev(fmt(3)) r_L[1](t label(Individualized Teaching)) alphau(fmt(3)) Psi[Uniqueness]") stats(alpha) nogap noobs label append



/***** The overall implementation scale ***/
factor lpq89_rec lpq90_rec lpq91_rec lpq93_rec lpq96_rec lpq98_rec lpq99_rec rec_lpq140 rec_lpq158 rec_lpq159 rec_lpq161 rec_lpq149 rec_lpq150 rec_lpq151, ml factor(4)
rotate, oblique oblimin
alpha  lpq89_rec lpq90_rec lpq91_rec lpq93_rec lpq96_rec lpq98_rec lpq99_rec rec_lpq140 rec_lpq158 rec_lpq159 rec_lpq161 rec_lpq149 rec_lpq150 rec_lpq151, item
estadd scalar alpha=r(alpha)
 estadd matrix alphau=r(Alpha)
 esttab using faktors_lp.rtf, cells("r_Ev(fmt(3)) r_L[1](t label(The overall implementation scale)) alphau(fmt(3)) Psi[Uniqueness]") stats(alpha) nogap noobs label replace


/*** THIS PART CREATES THE OUTCOME MEASUERES ***/
local year 2014 2015 2016 2017 2018
foreach q of local year {

capture gen varieretskole=.
egen varieretskole_z = rowtotal(lpq89_rec lpq90_rec lpq91_rec lpq93_rec) if year==`q', miss
qui su varieretskole_z 
replace varieretskole=varieretskole_z/r(max) if year==`q'
drop  varieretskole_z


capture gen faellesmaal=.
egen faellesmaal_z = rowtotal(lpq96_rec lpq98_rec lpq99_rec) if year==`q', miss
qui su faellesmaal_z 
replace faellesmaal=faellesmaal_z/r(max) if year==`q'
drop  faellesmaal_z

capture gen laeringsmaalstyret=.
egen laeringsmaalstyret_z = rowtotal( rec_lpq140 rec_lpq158 rec_lpq159 rec_lpq161) if year==`q' , miss
qui su laeringsmaalstyret_z 
replace laeringsmaalstyret=laeringsmaalstyret_z/r(max) if year==`q'
drop  laeringsmaalstyret_z

capture gen uddiff=.
egen uddiff_z = rowtotal(rec_lpq149 rec_lpq150 rec_lpq151) if year==`q', miss
qui su uddiff_z 
replace uddiff=uddiff_z/r(max) if year==`q'
drop  uddiff_z


cap gen samlet_imp2=.
egen samlet_imp2_z=rowtotal(lpq89_rec lpq90_rec lpq91_rec lpq93_rec lpq96_rec lpq98_rec lpq99_rec rec_lpq140 rec_lpq158 rec_lpq159 rec_lpq161 rec_lpq149 rec_lpq150 rec_lpq151) if year==`q',miss
qui su samlet_imp2_z
replace samlet_imp2=samlet_imp2_z/r(max) if year==`q' & !mi(samlet_imp2_z)
drop samlet_imp2_z

}

lab var varieretskole "Variation in Teaching"
lab var faellesmaal "Compliance with National Goals"
lab var laeringsmaalstyret "Individual Learning Goals"
lab var uddiff "Individualized Teaching"
lab var samlet_imp2 "Overall Implementation"



/****** ANSWERS OF "DO NOT KNOW" ARE IGNORED IN THE SCALES ***/
foreach var of varlist lpq257 lpq258 lpq259 lpq142 lpq143 lpq252 lpq253 lpq254 lpq244 {
gen `var'_1 = `var' if `var' <7
qui su `var'_1
gen `var'_2=(r(max)+1)-`var'_1
local x: variable label `var'
lab var `var'_2 "`x'"
}


/*** THIS STEP CREATES THE MEASURE OF PERCIEVED POLICY EFFICACY ***/
cap gen commitny2=.
egen commitny2_z=rowtotal(lpq257_2 lpq258_2 lpq259_2 ) , miss
qui su commitny2_z
replace commitny2=commitny2_z/r(max) if !mi(commitny2_z) & year>2015
drop commitny2_z

alpha lpq257_2 lpq258_2 lpq259_2 , item det


lab var commitny2 "Percieved Policy Efficacy"



estpost corr varieretskole faellesmaal laeringsmaalstyret uddiff, matrix
esttab using faktors_lp.rtf, unstack noobs not compress append


/**** THIS STEP CREATES VARIABLES FOR TEACHER'S AGE AND EXPERIENCE ***/ 

gen lp_alder=year-lpq04
gen undervisledertid=lpq14
replace undervisledertid=lpq13 if undervisledertid==.

/****** SELF-EFFICACY ****/
 
 factor  lpq207 lpq208 lpq209 lpq210 , ml
rotate, oblique oblimin
alpha  lpq207 lpq207 lpq208 lpq209 lpq210 , item
estadd scalar alpha=r(alpha)
 estadd matrix alphau=r(Alpha)
esttab using faktors_lp_SF.rtf, cells("r_Ev(fmt(3)) r_L[1](t label(Self-efficacy)) alphau(fmt(3)) Psi[Uniqueness]") stats(alpha) nogap noobs label append


foreach var of varlist  lpq207 lpq208 lpq209 lpq210 {
gen `var'_1 = `var' if `var' <7
qui su `var'_1
gen `var'_2=(r(max)+1)-`var'_1
local x: variable label `var'
lab var `var'_2 "`x'"
}


/*** THIS STEP CREATES THE MEASURE OF PERCIEVED SELF-EFFICACY ***/
cap gen selfeff=.
egen selfeff_z=rowtotal( lpq207_2 lpq208_2 lpq209_2 lpq210_2) , miss
qui su selfeff_z
replace selfeff=selfeff_z/r(max) if !mi(selfeff_z)
drop selfeff_z

lab var selfeff "Percieved Self-efficacy"


local year 2014 2015 2016 2017 2018
foreach q of local year {
estpost summarize self lpq196_rec commitny2 if year==`q' 
esttab . using "deskriptiv_lp_2.rtf", cells("mean(fmt(3)) sd(fmt(3)) count(fmt(0))") label ///
title(`"`q'"' ) noobs append mtitle

}
unicode convertfile deskriptiv_lp_2.rtf  deskriptiv_lp_3.rtf, srccallback(skip) dstencoding(Windows-1252) replace

eststo clear

destring instnr, replace

/***** This step identifies teachers, who have responded to the survey in each wave **/

/**** Teaching position (Danish or Math teacher, or both) ***/
gen stilling_ny=lpq05
drop if lpq05==4

/***** Some teachers change from being Danish or Math. teachers, to be both math and Danish teachers. This step corrects this. ***/
bys instnr lpq01 lpq02 lpq03 lpq04 (year) : replace stilling_ny=stilling_ny[_n+1] if stilling_ny != stilling_ny[_n+1] & !mi(stilling_ny[_n+1]) & !mi(stilling_ny)

/***** WE MAKE AN ID FROM TEACHERS INSTITUTION, SEX, DATE, MONTH AND YEAR OF BIRTH, ALONG WITH THE SUBJECT THEY TEACH****/
egen panellaerer=group(instnr lpq01 lpq02 lpq03 lpq04 stilling_ny)

egen panellaerer1518=group(instnr lpq01 lpq02 lpq03 lpq04)

/***** THE VARIABLES FOR DATE AND MONTH ARE NOT INCLUDED IN THE 2014-SURVEY ROUND. WE THEREFOR MAKE A DIFFERENT ID USING TEACHER SUBJECT, SEX AND YEAR OF BIRTH BY INSTITUTION ***/
bys instnr lpq01 lpq04 stilling_ny (year): replace panellaerer=panellaerer[_n+1] if year==2014 & panellaerer==.
bys instnr lpq01 lpq04 stilling_ny (year): replace panellaerer=panellaerer[_n+1] if year==2014 & panellaerer==.
bys instnr lpq01 lpq04 stilling_ny (year): replace panellaerer=panellaerer[_n+1] if year==2014 & panellaerer==.

save lpdata.dta, replace 


